package com.byt.patient.model;

import com.byt.base.model.BasePageModel;
import com.byt.netapi.bean.ResponseBean;
import com.byt.netapi.exception.ExceptionHandle;
import com.byt.netapi.observer.BaseObserver;
import com.byt.patient.api.RApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类名称：PatientMainModel
 * 类描述：
 */
public class PatientMainModel extends BasePageModel<ArrayList<String>> {

    public static final String PAGE = "page";

    private Map<String, Object> requestMap = null;

    public PatientMainModel(Map<String, Object> requestMap) {
        this.requestMap = requestMap;
    }

    @Override
    public void refresh() {
        isRefresh = true;
        load();
    }

    public void loadNexPage() {
        isRefresh = false;
        load();
    }

    @Override
    protected void load() {
        requestMap.put("per_page", "10");
        requestMap.put(PAGE, String.valueOf(isRefresh ? 1 : pageNumber));
        RApi.getInstance().obtainListData(new BaseObserver<ResponseBean<List<String>>>(this) {
            @Override
            public void onNext(ResponseBean<List<String>> enity) {
                // All observer run on main thread, no need to synchronize
                pageNumber = isRefresh ? 1 : pageNumber + 1;
                ArrayList<String> tempList = new ArrayList<>();
                tempList.addAll(enity.getData());
                loadSuccess(tempList, tempList.size() == 0, isRefresh, tempList.size() == 10);
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                e.printStackTrace();
                loadFail(e.message, isRefresh);
            }
        }, requestMap);
    }
}